layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;


    var tree_url = $('.tree_url').val();
    var total_level = $('.tree_url').attr('data-total_level');
    if (!total_level) {
        total_level = 1;
    }

    var Tree = function () {


        return {
            init: function () {
                if ($('#level-1').length > 0) {
                    get_data(1);
                }


                this.onEvent();
            },

            onEvent: function () {

                // 选择
                $(document).find('[id^=level-]').each(function (i) {
                    var _this = $(this);
                    //var id = _this.attr('id');
                    //var index = id.substr(6);
                    i ++;
                    form.on('select(level-' + i + ')', function (data) {
                        //var _this = $('#level-' + i);
                        get_data(i + 1, _this);

                        $('.level-' + i).val(_this.find('option:selected').text());
                        $('.level-' + i + '_id').val(_this.find('option:selected').val());
                        for (var j = i + 1; j <= total_level; j++) {
                            $('.level-' + j).val('');
                            $('.level-' + j + '_id').val('');
                        }
                    });
                });
            }
        };

    }();


    /**
     * 获取数据
     * @param select对象
     */
    function get_data(level, obj) {
        if (level > 1) {
            var parent_id = $(obj).val();
            if (!parent_id > 0) {
                return false;
            }
        } else {
            var parent_id = 0;
        }
        for (var j = level; j <= total_level; j++) {
            $('.level' + j).html('<option value="0">请选择</option>');
        }

        var url = tree_url + '?level=' + level + '&p_id=' + parent_id;
        $.ajax({
            type: "GET",
            url: url,
            data: {},
            //dataType:"json",
            error: function (request) {
                layer.msg("服务器繁忙!");
                return false;
            },
            success: function (data) {
                var options = '<option value="0">请选择</option>' + data;
                var elm = $('#level-' + level);
                elm.empty().html(options);

                if (elm.data('field') == 'id') {
                    var value_id = $('.level-' + level + '_id').val();
                    if (value_id) {
                        elm.val(value_id);
                        if (total_level != level) {
                            get_data(level + 1, elm);
                        }
                    }
                } else {
                    var value = $('.level-' + level).val();
                    if (value) {
                        elm.find("option:contains('" + value + "')").attr('selected', true);
                        if (total_level != level) {
                            get_data(level + 1, elm);
                        }
                    }
                }

                form.render();
            }
        });
        return false;
    }

    $(function () {
        Tree.init();
    });
});
